package com.demo.server;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import com.demo.thrift.ThriftMysqlService;

/**
 * 阻塞式单线程服务
 * @author HuangKe
 * @date 2017年3月7日
 * @company Copyright © 2007-2016 www.zgxcw.com All Rights Reserved
 */
public class BlockSingleThreadServer {
	
	private static final int port = 8899;
	
	public static void main(String[] args) {
		 //1. 创建processor
		TProcessor tprocessor = new ThriftMysqlService.Processor<ThriftMysqlService.Iface>(new ThriftMyqlServiceImpl());
		try {
			//2. 创建transport 阻塞通信
			TServerSocket serverTransport = new TServerSocket(port);
			//3. 创建protocol
			TBinaryProtocol.Factory protocol = new TBinaryProtocol.Factory();
			//将processor transport protocol加入到服务器server中
			TServer.Args arg = new TServer.Args(serverTransport);
			arg.processor(tprocessor);
			arg.protocolFactory(protocol);
			//定义服务器类型 设定参数
			TServer server = new TSimpleServer(arg);
			//开启服务
			System.out.println("---------开启阻塞式单线程服务---------");
			server.serve();
			System.out.println("---------开启阻塞式单线程服务---------");
		} catch (TTransportException e) {
			e.printStackTrace();
		}
	
	}
}
